package com.farm.shop.controller;

import com.farm.common.utils.PageUtils;
import com.farm.common.utils.Query;
import com.farm.common.utils.R;
import com.farm.shop.domain.FarmBannerDO;
import com.farm.shop.service.FarmBannerService;
import com.farm.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 广告轮播图
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-06 19:44:48
 */

@Controller
@RequestMapping("/farm/farmBanner")
public class FarmBannerController {
    @Autowired
    private FarmBannerService farmBannerService;

    /**
     * 用户版 首页广告位展示  查询 status 为 1 的上线的广告
     * status 1上线  0下线
     *
     * @return List<BannerEntity>
     */
    @ResponseBody
    @GetMapping(value = "/app/banner")
    public Result getAppBannersBySort() {
        return Result.success(farmBannerService.getAppBannersBySort());
    }

    @GetMapping()
    @RequiresPermissions("farm:farmBanner:farmBanner")
    String FarmBanner() {
        return "farm/farmBanner/farmBanner";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("farm:farmBanner:farmBanner")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<FarmBannerDO> farmBannerList = farmBannerService.list(query);
        int total = farmBannerService.count(query);
        PageUtils pageUtils = new PageUtils(farmBannerList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("farm:farmBanner:add")
    String add() {
        return "farm/farmBanner/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("farm:farmBanner:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        FarmBannerDO farmBanner = farmBannerService.get(id);
        model.addAttribute("farmBanner", farmBanner);
        return "farm/farmBanner/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("farm:farmBanner:add")
    public R save(FarmBannerDO farmBanner) {
        if (farmBannerService.save(farmBanner) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("farm:farmBanner:edit")
    public R update(FarmBannerDO farmBanner) {
        farmBannerService.update(farmBanner);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("farm:farmBanner:remove")
    public R remove(Long id) {
        if (farmBannerService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("farm:farmBanner:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        farmBannerService.batchRemove(ids);
        return R.ok();
    }

}
